﻿using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Model.Core;
using DevExpress.ExpressApp.Model.NodeGenerators;

namespace EasyXaf.ImageViewers
{
    public class ImageViewerNodesGeneratorUpdater : ModelNodesGeneratorUpdater<ModelDetailViewItemsNodesGenerator>
    {
        public override void UpdateNode(ModelNode node)
        {
            if (node is IModelViewItems viewItems)
            {
                foreach (var viewItem in viewItems)
                {
                    if (viewItem is IModelPropertyEditor propertyEditorModel && propertyEditorModel is IModelImageViewer imageViewerModel)
                    {
                        var imageViewerAttribute = propertyEditorModel.ModelMember.MemberInfo.FindAttribute<ImageViewerAttribute>();
                        if (imageViewerAttribute != null)
                        {
                            imageViewerModel.ImageViewerWidth = imageViewerAttribute.Width;
                            imageViewerModel.ImageViewerHeight = imageViewerAttribute.Height;
                        }
                    }
                }
            }
        }
    }
}
